/*
 ============================================================================
 Name        : time_schedule.c
 Author      : chenf
 Version     : 1.0
 Copyright   : loongsec
 Description : time_schedule crud in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
#include "cgi.h"
#include "cookie.h"


void putJSON(char **azResult, int nrow, int ncolumn);
void putHTML(char **azResult, int nrow, int ncolumn);


int main(void) {
    sqlite3  *db  = NULL;
    int      nrow=0,ncolumn=0;
    char     **azResult = NULL;
    char     *zErrMsg = 0;
    char     sql[500];
    char     *input;
    char     *req_method;
    int      rc;

    if(isvalid_cookie()!=0){
        return 0;
    }

    rc = sqlite3_open(PATH_DATABASE,&db);
    if (rc) {
        fprintf(stderr,"Can't open database:%s\n",sqlite3_errmsg(db));
        sqlite3_close(db);
        return 0;

    } else {

        req_method = getenv("REQUEST_METHOD");

        FILE* fp = (FILE*) stdin;
        input = getcgidata(fp, req_method);

        PARAM id = getParam(input, "id");
        //

        PARAM dataType = getParam(input, "dataType");

        if( strcmp(id.value, "") != 0 && strcmp(id.value, "null") != 0 ){
            sprintf(sql, "select * from [TB_TIME_SCHEDULE] \
             where SCHEDULE_INDEX = '%s' ", id.value);
        } else {
            sprintf(sql, "select * from [TB_TIME_SCHEDULE]");
        }

        //printf("%s", sql);

        rc = sqlite3_get_table(db,sql,&azResult,&nrow,&ncolumn,&zErrMsg);
        if (rc != SQLITE_OK) {
            fprintf(stderr,"SQL error:%s\n", zErrMsg);
            sqlite3_close(db);
            return 0;

        } if (*azResult != NULL) {
            // choose data format
            if( strcmp(dataType.value, "json") == 0 ){
                putJSON(azResult, nrow, ncolumn);
            } else {
                putHTML(azResult, nrow, ncolumn);
            }
        }
    }

    sqlite3_close(db);
    return 0;
}


void putJSON(char **azResult, int nrow, int ncolumn){
    int i=0, j=0, n=0;
    printf("Content-type:text/json;charset=UTF-8\n\n");
    printf("{ \"rows\": \"%d\", \"data\": [", nrow);
    for(i=1; i<=nrow; i++) {
        printf("{");
        char *phaseIndex = {0};
        for(j=0; j<ncolumn; j++) {
            if(j==0){
                phaseIndex = azResult[ (i*ncolumn) + j ];
            }
            printf("\"%s\": \"", azResult[j]);
            printf("%s", azResult[ (i*ncolumn) + j ]);
            if(j == ncolumn-1)
                printf("\"");
            else
                printf("\",");
            n++;
        }
        if(i == nrow)
            printf("}\n\n");
        else
            printf("},\n\n");
    }
    printf("]}");
}


void putHTML(char **azResult, int nrow, int ncolumn){
    int i=0, j=0, n=0;
    printf("Content-type:text/html;charset=UTF-8\n\n");
    printf("<table res=\"%d\" id=\"logTbl\" class=\"dataGrid\">", nrow);

    printf("<tr>\
        <th>调度计划号</th>\
        <th>调度月</th>\
        <th>按周调度日</th>\
        <th>按月调度日</th>\
        <th>时段表号</th>\
        <th>修改时间</th>\
        <th>选项</th></tr>\n\n");

    for(i=1; i<=nrow; i++) {
        printf("<tr>");
        char *index = {0};
        for(j=0; j<ncolumn; j++) {
            if(j==0){
                index = azResult[ (i*ncolumn) + j ];
            }
            printf("<td>");
            printf("%s", azResult[ (i*ncolumn) + j ]);
            printf("</td>");
            n++;
        }

        printf("<td>\
                    <input type=\"button\" class=\"edit-btn\" title=\"编辑\" onclick=\"editSchedule(%s)\"   />\
                    <input type=\"button\" class=\"dele-btn\" title=\"删除\" onclick=\"deleteSchedule(%s)\" />\
              </td>", index, index);

        printf("<tr>\n\n");
    }
    printf("</table>");
}


